From 75390b2d602ca73156190350b9b62a8f870c801d Mon Sep 17 00:00:00 2001 From: Matthias Clasen Date: Tue, 31 May 2011 18:28:55 -0400 Subject: [PATCH] Sort GtkApplication windows by most-recently-focused Based on a patch by Matt Barnes, https://bugzilla.gnome.org/show_bug.cgi?id=624539 --- gtk/gtkapplication.c | 40 +++++++++++++++++++++++++++++++++++++--- 1 file changed, 37 insertions(+), 3 deletions(-) diff --git a/gtk/gtkapplication.c b/gtk/gtkapplication.c index cf57bf28f5..d6287d9c9b 100644 --- a/gtk/gtkapplication.c +++ b/gtk/gtkapplication.c @@ -67,6 +67,25 @@ struct _GtkApplicationPrivate GList *windows; }; +static gboolean +gtk_application_focus_in_event_cb (GtkWindow *window, + GdkEventFocus *event, + GtkApplication *application) +{ + GtkApplicationPrivate *priv = application->priv; + GList *link; + + /* Keep the window list sorted by most-recently-focused. */ + link = g_list_find (priv->windows, window); + if (link != NULL && link != priv->windows) + { + priv->windows = g_list_remove_link (priv->windows, link); + priv->windows = g_list_concat (link, priv->windows); + } + + return FALSE; +} + static void gtk_application_startup (GApplication *application) { @@ -216,6 +235,10 @@ gtk_application_add_window (GtkApplication *application, priv->windows = g_list_prepend (priv->windows, window); gtk_window_set_application (window, application); g_application_hold (G_APPLICATION (application)); + + g_signal_connect (window, "focus-in-event", + G_CALLBACK (gtk_application_focus_in_event_cb), + application); } } @@ -240,13 +263,18 @@ gtk_application_remove_window (GtkApplication *application, GtkWindow *window) { GtkApplicationPrivate *priv; + GList *link; g_return_if_fail (GTK_IS_APPLICATION (application)); priv = application->priv; - if (g_list_find (priv->windows, window)) + link = g_list_find (priv->windows, window); + if (link) { - priv->windows = g_list_remove (priv->windows, window); + g_signal_handlers_disconnect_by_func (window, + gtk_application_focus_in_event_cb, + application); + priv->windows = g_list_remove_link (priv->windows, link); g_application_release (G_APPLICATION (application)); gtk_window_set_application (window, NULL); } @@ -258,7 +286,13 @@ gtk_application_remove_window (GtkApplication *application, * * Gets a list of the #GtkWindows associated with @application. * - * The list that is returned should not be modified in any way. + * The list is sorted by most recently focused window, such that the first + * element is the currently focused window. (Useful for choosing a parent + * for a transient window.) + * + * The list that is returned should not be modified in any way. It will + * only remain valid until the next focus change or window creation or + * deletion. * * Returns: (element-type GtkWindow) (transfer none): a #GList of #GtkWindow * -- 2.30.2